package cn.yunhe.util;

import cn.yunhe.entity.StudentProblem;
import org.dom4j.*;
import java.util.*;

/**
 * Created by 47496 on 2018/5/28.
 */
public class ListToXml {


    /**
     * list转xml格式字符串
     *
     * @param list
     * @return
     */
    public static String listToXml(List<StudentProblem> list) {
        //String 存储
        StringBuffer strXml = new StringBuffer();
        //不能为空
        if (list == null || list.size() == 0 || list.get(0) == null) {
            return null;
        }

        //拼接成xml格式
        strXml.append("<problems>");
        //循环list
        for (StudentProblem p : list) {
            strXml.append("<problem>");
            //id
            strXml.append("<pid>").append(p.getPid()).append("</pid>");
            //题名
            strXml.append("<pname>").append(p.getPname()).append("</pname>");
            //选项

            //遍历选项map集合
            if (p.getPchoice() != null){
                Set<Map.Entry<String, String>> entrys = p.getPchoice().entrySet();
                strXml.append("<pchoice>");
                for (Map.Entry<String, String> entry : entrys) {
                    strXml.append("<map key=\"").append(entry.getKey()).append("\">");
                    strXml.append(entry.getValue());
                    strXml.append("</map>");
                }
                strXml.append("</pchoice>");
            }else {
                strXml.append("<pchoice>").append("</pchoice>");
            }

            //遍历图片map集合
            if (p.getImages() != null){
                Set<Map.Entry<String,String>> images = p.getImages().entrySet();
                strXml.append("<images>");
                for (Map.Entry<String, String> entry : images) {
                    strXml.append("<map key=\"").append(entry.getKey()).append("\">");
                    strXml.append(entry.getValue());
                    strXml.append("</map>");
                }
                strXml.append("</images>");
            }else {
                strXml.append("<images>").append("</images>");
            }

            //正确答案
            strXml.append("<correctAnswer>").append(p.getCorrectAnswer()).append("</correctAnswer>");
            //学生答案
            strXml.append("<studentAnswer>").append(arrToStr(p.getStudentAnswer())).append("</studentAnswer>");
            //试题解析
            strXml.append("<resolve>").append(p.getResolve()).append("</resolve>");
            //试题类型
            strXml.append("<type>").append(p.getType()).append("</type>");
            //难度等级
            strXml.append("<grade>").append(p.getGrade()).append("</grade>");
            strXml.append("</problem>");
        }
        strXml.append("</problems>");
        return strXml.toString();
    }

    public static List<StudentProblem> xmlToList(String xml) {
        List<StudentProblem> list = new ArrayList<>();

        try {
            //解析xml文本形式，创建document对象
            Document document = DocumentHelper.parseText(xml);
            //获取xml的根节点
            Element root = document.getRootElement();
            //获得某一节点下的所有子节点
            List nodes = root.elements("problem");
            //遍历子节点
            for (Iterator it = nodes.iterator(); it.hasNext(); ) {
                //某一节点
                Element ele = (Element) it.next();

                //字符串规范
                String num = "1234567890";
                StudentProblem sp = new StudentProblem();

                //id
                if (ele.elementText("pid") != null) {
                    sp.setPid(Integer.parseInt(ele.elementText("pid")));
                }
                //问题
                sp.setPname(ele.elementText("pname"));
                /*
                * 选项
                * */
                Map<String, String> pc = new HashMap<>();
                //获取节点的集合
                Element pchoices = ele.element("pchoice");
                List<Element> pchoice = pchoices.elements("map");
                for (Iterator i = pchoice.iterator(); i.hasNext(); ) {
                    Element e = (Element) i.next();
                    String key = e.attributeValue("key");
                    String value = e.getText();
                    pc.put(key,value);
                }
                sp.setPchoice(pc);
                /*
                * 图片*/
                LinkedHashMap<String,String> im = new LinkedHashMap<>();
                //获取节点的集合
                Element images = ele.element("images");
                List<Element> image = images.elements("map");
                for (Iterator i = image.iterator(); i.hasNext(); ) {
                    Element e = (Element) i.next();
                    String key = e.attributeValue("key");
                    String value = e.getText();
                    System.out.println(key+"===="+value);
                    im.put(key,value);
                }
                sp.setImages(im);
                //正确答案
                sp.setCorrectAnswer(ele.elementText("correctAnswer"));
                //学生答案
                sp.setStudentAnswer(ele.elementText("studentAnswer").split(","));
                //试题解析
                sp.setResolve(ele.elementText("resolve"));
                //试题类型
                sp.setType(ele.elementText("type"));
                //难度等级
                if (num.indexOf(ele.elementText("grade")) != -1) {
                    sp.setGrade(Integer.parseInt(ele.elementText("grade")));
                }
                list.add(sp);

            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 规范Element的asXMl方法返回的字符串形式
     *
     * @param e
     * @return
     */
    public static String subStringXml(Element e) {
        if (e == null) {
            return null;
        }
        return e.asXML().substring(3, e.asXML().length() - 4);
    }

    /**
     * 字符串数组转字符串
     *
     * @param ans
     * @return
     */
    public static String arrToStr(String[] ans) {
        String str = null;
        if (ans != null) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < ans.length; i++) {
                sb.append(ans[i]);
                if (i != ans.length - 1) {
                    sb.append(",");
                }
                str = sb.toString();
            }
        }
        return str;
    }

    /**
     * 字符串截取数组，获取试卷中每个类型的分值
     * @param tname
     * @param strucs
     * @return
     */
    public static String getTypeScore(String tname,String strucs){
        String[] types = strucs.split(",");
        for (int i = 0;i<types.length;i++){
            String[] sc = types[i].split("-");
            if(tname.equals(sc[0])){
                return sc[1];
            }
        }
        return null;
    }

    /**
     * 删除字符串的第一位
     * @param arr
     * @return
     */
    public static String[] removeArray(String[] arr){
        String[] strs = new String[arr.length-1];
        for (int i = 0;i<strs.length;i++){
            strs[i] = arr[i+1];
        }
        return strs;
    }


}